package com.txzt.gaea.base.service.impl;

import com.txzt.base.constant.RtnConstant;
import com.txzt.base.core.RtnInfo;
import com.txzt.gaea.api.client.AdminUserRemoteService;
import com.txzt.gaea.base.dto.DictDetailQueryDTO;
import com.txzt.gaea.base.model.DictDetail;
import com.txzt.gaea.base.service.DictDetailService;
import com.txzt.gaea.base.vo.DictDetailVo;
import com.txzt.gaea.common.base.service.impl.BaseServiceImpl;
import com.txzt.gaea.common.enums.IsDeleteEnum;
import com.txzt.gaea.common.page.PageInfoBT;
import com.txzt.gaea.dto.UserSingleDetailQueryDTO;
import lombok.extern.log4j.Log4j2;
import org.beetl.sql.core.engine.PageQuery;
import org.beetl.sql.core.query.LambdaQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Log4j2
@Service
public class DictDetailServiceImpl extends BaseServiceImpl<DictDetail> implements DictDetailService {

    @Autowired
    private AdminUserRemoteService adminUserRemoteService;

    @Override
    public RtnInfo<PageInfoBT<DictDetailVo>> list(DictDetailQueryDTO dictDetailQueryDTO){
        PageQuery<DictDetailVo> pageQuery = new PageQuery<>(dictDetailQueryDTO.getOffset(), dictDetailQueryDTO.getLimit(), dictDetailQueryDTO);
        PageQuery<DictDetailVo> result = this.sqlManager.pageQuery("dictDetail.selectPageList", DictDetailVo.class, pageQuery);
        //为空直接返回
        if (CollectionUtils.isEmpty(result.getList())) {
            return RtnInfo.success(new PageInfoBT<>(result));
        }
        result.getList().forEach(item -> {
            UserSingleDetailQueryDTO userSingleDetailQueryDTO = new UserSingleDetailQueryDTO();
            userSingleDetailQueryDTO.setUserId(item.getCreateBy());
            item.setCreateName(adminUserRemoteService.singleDetailQuery(userSingleDetailQueryDTO).getData().getName());
            userSingleDetailQueryDTO.setUserId(item.getUpdateBy());
            item.setUpdateName(adminUserRemoteService.singleDetailQuery(userSingleDetailQueryDTO).getData().getName());
        });
        PageInfoBT<DictDetailVo> re = new PageInfoBT<>(result);
        rebuild(re);
        return RtnInfo.success(re);
    }

    @Override
    public int save(DictDetail dictDetail){
        return this.sqlManager.insert(dictDetail);
    }

    @Override
    public int update(DictDetail dictDetail){
        return this.sqlManager.updateTemplateById(dictDetail);
    }

    @Override
    public Integer getLastSort(Long dictId){
        DictDetail dictDetail = new DictDetail();
        dictDetail.setDictId(dictId);
        List<DictDetail> dictDetailList = this.sqlManager.select("dictDetail.selectDictSort", DictDetail.class, dictDetail);
        if (0 == dictDetailList.size() || null == dictDetailList.get(0).getSort()) {
            return 0;
        } else {
            return dictDetailList.get(0).getSort();
        }
    }

    @Override
    public boolean addCheckValue(DictDetail dictDetail){
        LambdaQuery<DictDetail> query = sqlManager.lambdaQuery(DictDetail.class);
        List<DictDetail> orgDictDetailList =
                query.andEq(DictDetail::getValue,dictDetail.getValue())
                        .andEq(DictDetail::getDictId,dictDetail.getDictId())
                        .select();
        return 0 >= orgDictDetailList.size();
    }

    @Override
    public boolean updateCheckValue(DictDetail dictDetail){
        LambdaQuery<DictDetail> query = sqlManager.lambdaQuery(DictDetail.class);
        List<DictDetail> orgDictDetailList = query.andEq(DictDetail::getValue,dictDetail.getValue())
                .andEq(DictDetail::getDictId,dictDetail.getDictId())
                .andEq(DictDetail::getIsDelete, IsDeleteEnum.NOTDELETE.getCode())
                .andNotEq(DictDetail::getId,dictDetail.getId()).select();
        return 0 >= orgDictDetailList.size();
    }

    @Override
    public int delete(Long id){
        DictDetail dictDetail = new DictDetail();
        dictDetail.setId(id);
        dictDetail.setIsDelete(IsDeleteEnum.DELETE.getCode());
        return this.sqlManager.updateTemplateById(dictDetail);
    }

    @Override
    public RtnInfo<DictDetail> getDictDetail(Long id){
        DictDetail dictDetail = this.selectById(id);
        if (null == dictDetail){
            return RtnInfo.error(RtnConstant.Code.PARAMS_REEOR,"未查到相关信息，请检查上传数据是否正确");
        } else {
          return  RtnInfo.success(dictDetail);
        }
    }

    private void rebuild(PageInfoBT<DictDetailVo> re){
        List<DictDetailVo> list = re.getRows();
        if(CollectionUtils.isEmpty(list)){
            re.setTotal(0);
            return;
        }
//        re.setTotal(list.size());
    }

    @Override
    public String getCodeFlag(String dictCode,String value){
        Map<String,Object> param = new HashMap<>();
        param.put("dictCode",dictCode);
        param.put("value",value);
        DictDetail dictDetail = this.sqlManager.selectSingle("dictDetail.selectCodeFlagByDictCodeAndValue",param,DictDetail.class);
        return dictDetail.getCodeFlag();
    }
}
